home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / TNSERV.C < prev    next >
C/C++ Source or Header  |  1988-07-28  |  3KB  |  107 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "internet.h"
  6. #include "icmp.h"
  7. #include "netuser.h"
  8. #include "tcp.h"
  9. #include "telnet.h"
  10. #include "session.h"
  11.  
  12. struct tcb *tnet_tcb;
  13. telnet_start(argc,argv)
  14. char *argv[];
  15. {
  16.     struct socket lsocket;
  17.     extern int32 ip_addr;
  18.     void tnet_state();
  19.     void t_state(),rcv_char();
  20.  
  21.     /* Incoming Telnet */
  22.     lsocket.address = ip_addr;
  23.     if(argc < 2)
  24.         lsocket.port = TELNET_PORT;
  25.     else
  26.         lsocket.port = atoi(argv[1]);
  27.     tnet_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,rcv_char,NULLVFP,tnet_state,0,(char *)NULL);
  28. }
  29. /* Handle incoming Telnet connect requests by creating a Telnet session,
  30.  * then change upcall vector so it behaves like an ordinary Telnet session.
  31.  * 
  32.  */
  33. static void
  34. tnet_state(tcb,old,new)
  35. struct tcb *tcb;
  36. char old,new;
  37. {
  38.     struct telnet *tn;
  39.     struct session *s,*newsession();
  40.     void t_state();
  41.     char *a;
  42.  
  43.     switch(new){
  44.     case ESTABLISHED:
  45.         log(tcb,"open Telnet");
  46.         /* Allocate a session descriptor */
  47.         if((s = newsession()) == NULLSESSION){
  48.             printf("\007Incoming Telnet call from %s refused; too many sessions\n",
  49.              psocket(&tcb->conn.remote));
  50.             fflush(stdout);
  51.             sndmsg(tcb,"Call rejected; too many sessions on remote system\n");
  52.             close_tcp(tcb);
  53.             return;
  54.         }
  55.         a = inet_ntoa(tcb->conn.remote.address);
  56.         if((s->name = malloc((unsigned)strlen(a)+1)) != NULLCHAR)
  57.             strcpy(s->name,a);
  58.         s->type = TELNET;
  59.         s->parse = send_tel;
  60.         /* Create and initialize a Telnet protocol descriptor */
  61.         if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN){
  62.             printf("\007Incoming Telnet call refused; no space\n");
  63.             fflush(stdout);
  64.             sndmsg(tcb,"Call rejected; no space on remote system\n");
  65.             close_tcp(tcb);
  66.             s->type = FREE;
  67.             return;
  68.         }
  69.         tn->session = s;    /* Upward pointer */
  70.         tn->state = TS_DATA;
  71.         s->cb.telnet = tn;    /* Downward pointer */
  72.  
  73.         tcb->user = (char *)tn;    /* Upward pointer */
  74.         tn->tcb = tcb;        /* Downward pointer */
  75.         printf("\007Incoming Telnet session %lu from %s\n",
  76.          (long)(s - sessions),psocket(&tcb->conn.remote));
  77.         fflush(stdout);
  78.         tcb->s_upcall = t_state;
  79.         return;
  80.     case CLOSED:
  81.         /* This will only happen if the connection closed before
  82.          * the session was set up, e.g., if we refused it because
  83.          * there were too many sessions, or if the server is being
  84.          * shut down.
  85.          */
  86.         if(tcb == tnet_tcb)
  87.             tnet_tcb = NULLTCB;
  88.         del_tcp(tcb);
  89.         break;
  90.     }
  91. }
  92. telnet_stop()
  93. {
  94.     if(tnet_tcb != NULLTCB)
  95.         close_tcp(tnet_tcb);
  96. }
  97. static
  98. sndmsg(tcb,msg)
  99. struct tcb *tcb;
  100. char *msg;
  101. {
  102.     struct mbuf *bp;
  103.  
  104.     bp = qdata(msg,(int16)strlen(msg));
  105.     send_tcp(tcb,bp);
  106. }
  107.